<?php
namespace Home\Biz;

class PromoterBiz{
	
	public static function generateShareCode(){
		$seq = 100000 + CommonBiz::getNewSeq('share_code');
		return 'FZ'.$seq;
	}
	
	public static function generateUserId(){
		$seq = 10000000 + CommonBiz::getNewSeq('user_id');
		return 'U'.$seq;
	}
	
	public static function generatePromoterId(){
		$seq = 100000 + CommonBiz::getNewSeq('promoter_id');
		return 'PR'.$seq;
		
	}
	
	
	public static function updateInfo($promoter_en){
		//参数验证 start
		
		$exist_promoter_en = M('promoterInfo')->where(array('promoter_id'=>$promoter_en['promoter_id']))->find();
		
		if(empty($exist_promoter_en)){
			return '不存在的用户';
		}
		
		if(empty($promoter_en['sex'])){
			return '性别选择错误';
		}
		if(empty($promoter_en['real_name'])){
			return '真实姓名不能为空';
		}
		
		if(empty($promoter_en['alipay_id'])){
			return '支付宝账户不能为空';
		}
		if(empty($promoter_en['cellphone'])){
			return '手机不能为空';
		}
		if(!StringBiz::isCellphone($promoter_en['cellphone'])){
			return '手机格式不符合';
		}
		
		if(empty($promoter_en['id_card_no'])){
			return '身份证号码不能为空';
		}
		if(!StringBiz::isIdentity($promoter_en['id_card_no'])){
			return '身份证格式不正确';
		}
		
		if($exist_promoter_en['promoter_type'] == 2){
			if(empty($promoter_en['business_license_no'])){
				return '营业执照号码不能为空';
			}
		}
		if(empty($promoter_en['district_id'])){
			return '省市区选择错误';
		}
		if(empty($promoter_en['address'])){
			return '地址不能为空';
		}
		
		if(!StringBiz::isAddress($promoter_en['address'])){
			return '地址信息不正确';
		}
		
		//校验手机号是否存在
		
		if($exist_promoter_en['cellphone'] != $promoter_en['cellphone']){
			$exist_cellphone_promoter_en = M('promoterInfo')->where(array('cellphone'=>$promoter_en['cellphone']))->find();
			if(!empty($exist_cellphone_promoter_en)){
				return '这个手机号码已经注册过了';
			}
		}
		
		
		//省市区
		$district_id = $promoter_en['district_id'];
		$lst_region_en = M('', '', 'DB_system')->query("
				SELECT * FROM province p
				INNER JOIN city c
				ON p.`province_id` = c.`province_id`
				INNER JOIN district d
				ON c.`city_id` = d.`city_id`
				WHERE district_id = '%s'
				", $district_id);
		
		
		if(empty($lst_region_en)){
			return '错误的区域选择';
		}
		else{
			$region_en = $lst_region_en[0];
		}
		//参数验证 end
		
		
		$promoter_en['province_id']		= $region_en['province_id'];
		$promoter_en['province_name']	= $region_en['province_name'];
		$promoter_en['city_id']			= $region_en['city_id'];
		$promoter_en['city_name']		= $region_en['city_name'];
		$promoter_en['district_id']		= $region_en['district_id'];
		$promoter_en['district_name']	= $region_en['district_name'];
		
		//更新promoter_info
		M('promoterInfo')->where(array('promoter_id'=>$promoter_en['promoter_id']))->save($promoter_en);
		
		//更新user_info
		$update_user_info_en = array(
				'user_name' => $promoter_en['real_name'],
				'real_name' => $promoter_en['real_name'],
				'cellphone' => $promoter_en['cellphone'],
				'sex' 		=> $promoter_en['sex'],
				'province_id'	=> $promoter_en['province_id'],
				'province_name' => $promoter_en['province_name'],
				'city_id'		=> $promoter_en['city_id'],
				'city_name' 	=> $promoter_en['city_name'],
				'district_id'	=> $promoter_en['district_id'],
				'district_name' => $promoter_en['district_name'],
				'address'		=> $promoter_en['address'],
				'update_user'	=> $promoter_en['update_user'],
				'update_date'	=> date("y-m-d H:i:s", time())
		);
		
		M('userInfo', '', 'DB_trade')->where(array('user_id'=>$exist_promoter_en['user_id']))->save($update_user_info_en);
	}
	
	
	public static function updatePwd($promoter_id, $old_pwd, $new_pwd, $new_pwd2){
		// 数据校验start
		if($new_pwd != $new_pwd2){
			return '两次输入的密码不一致';
		}
		
		$promoter_en = M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->find();
		if(empty($promoter_en)){
			return '用户不存在';
		}
		else{
			if($promoter_en['status'] != 1){
				return '用户状态无效，不允许提现';
			}
		}
		
		//验证老密码
		$old_encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_id.$old_pwd);
		if($promoter_en['password'] != $old_encrypt_password){
			return '老密码错误';
		}
		// 数据校验end
		
		//更新密码
		$new_encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_id.$new_pwd);
		
		M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->setField('password', $new_encrypt_password);
		
	}
	
	/**
	 * 重置用户密码为123456
	 * @param unknown $promoter_id
	 * @return string
	 */
	public static function resetPwd($promoter_id){
		// 数据校验start
		
		// 数据校验end
	
		$new_pwd = '123456';
		
		//更新密码
		$new_encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_id.$new_pwd);
		
		M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->setField('password', $new_encrypt_password);
	
	}
	
	public static function searchAmountList($param_en){
		// 参数检查start
		if(empty($param_en['page_index'])){
			$param_en['page_index'] = 1;
		}
		
		if(empty($param_en['page_size'])){
			$param_en['page_size'] = C('PromoterConfig.amountList_page_search_default_size');
		}
		// 参数检查end
		
		// 搜索条件start
		$condition_array = array();
		if(!empty($param_en['promoter_id'])){
			$condition_array['promoter_id'] = array('eq', $param_en['promoter_id']);
		}
		// 搜索条件end
		
		$return_en = array();
		$total_count = M('promoterAmount')
		->where($condition_array)
		->count();
		$return_en['total_count'] = $total_count;
		
		
		$lst_amount_en = M('promoterAmount')
		->page($param_en['page_index'], $param_en['page_size'])
		->order('create_date DESC')
		->where($condition_array)
		->select();
		
		
		foreach($lst_amount_en as $key => $en){
			$lst_amount_en[$key]['amount_kind_text'] = StringBiz::getPromoterAmountAmountKindText($en['amount_kind']);
			$lst_amount_en[$key]['amount_type_text'] = StringBiz::getPromoterAmountAmountTypeText($en['amount_type']);
		}
		$return_en['lst_amount_en'] = $lst_amount_en;
		
		$total_page = 0;
		
		$rem = $total_count % $param_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $param_en['page_size'];
		}
		else{
			$total_page = floor($total_count / $param_en['page_size']) + 1;
		}
		$return_en['total_page'] = $total_page;
		
		return $return_en;
	}
	
	
	public static function searchWithdrawAmountList($param_en){
		// 参数检查start
		if(empty($param_en['page_index'])){
			$param_en['page_index'] = 1;
		}
	
		if(empty($param_en['page_size'])){
			$param_en['page_size'] = C('PromoterConfig.amountList_page_search_default_size');
		}
		// 参数检查end
	
		// 搜索条件start
		$condition_array = array();
		if(!empty($param_en['promoter_id'])){
			$condition_array['promoter_id'] = array('eq', $param_en['promoter_id']);
		}
		// 搜索条件end
	
		$return_en = array();
		$total_count = M('promoterWithdrawAmount')
		->where($condition_array)
		->count();
		$return_en['total_count'] = $total_count;
	
	
		$lst_amount_en = M('promoterWithdrawAmount')
		->page($param_en['page_index'], $param_en['page_size'])
		->order('create_date DESC')
		->where($condition_array)
		->select();
	
		foreach($lst_amount_en as $key => $en){
			$lst_amount_en[$key]['amount_kind_text'] = StringBiz::getPromoterAmountAmountKindText($en['amount_kind']);
			$lst_amount_en[$key]['amount_type_text'] = StringBiz::getPromoterAmountAmountTypeText($en['amount_type']);
		}
		$return_en['lst_amount_en'] = $lst_amount_en;
	
		$total_page = 0;
		
		$rem = $total_count % $param_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $param_en['page_size'];
		}
		else{
			$total_page = floor($total_count / $param_en['page_size']) + 1;
		}
		$return_en['total_page'] = $total_page;
	
		return $return_en;
	}
	
	public static function searchWithdrawList($param_en){
		// 参数检查start
		if(empty($param_en['page_index'])){
			$param_en['page_index'] = 1;
		}
	
		if(empty($param_en['page_size'])){
			$param_en['page_size'] = C('PromoterConfig.withdrawList_page_search_default_size');
		}
		// 参数检查end
	
		// 搜索条件start
		$condition_array = array();
		if(!empty($param_en['promoter_id'])){

			$condition_array['promoter_id'] = array('eq', $param_en['promoter_id']);
		}

		if(!empty($param_en['promoter_name'])){
			$condition_array['promoter_name'] = array('eq', $param_en['promoter_name']);
		}

        if(!empty($param_en['status'])){
            $condition_array['status'] = array('eq', $param_en['status']);
        }
		// 搜索条件end

	
		$return_en = array();
		$total_count = M('promoterWithdraw')
		->where($condition_array)
		->count();
		$return_en['total_count'] = $total_count;
	
	
		$lst_withdraw_en = M('promoterWithdraw')
		->page($param_en['page_index'], $param_en['page_size'])
		->order('create_date DESC')
		->where($condition_array)
		->select();

	
		foreach($lst_withdraw_en as $key => $en){
			$lst_withdraw_en[$key]['status_text'] = StringBiz::getWithdrawStatusText($en['status']);
			
		}
		$return_en['lst_withdraw_en'] = $lst_withdraw_en;
	
		$total_page = 0;
	
		$rem = $total_count % $param_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $param_en['page_size'];
		}
		else{
			$total_page = floor($total_count / $param_en['page_size']) + 1;
		}
		$return_en['total_page'] = $total_page;
	
		return $return_en;
	}
	
	
	
	public static function searchPromoterList($promoter_en){
		
		// 参数检查start
		if(empty($promoter_en['page_index'])){
			$promoter_en['page_index'] = 1;
		}
		
		if(empty($promoter_en['page_index'])){
			$promoter_en['page_size'] = C('PromoterConfig.listpage_search_default_size');
		}
		// 参数检查end
		
		
		// 搜索条件start
		$condition_array = array();
		if(!empty($promoter_en['cellphone'])){
			$condition_array['cellphone'] = array('like', "%".$promoter_en['cellphone']."%");
		}
		
		if(!empty($promoter_en['promoter_type'])){
			$condition_array['promoter_type'] = array('eq', $promoter_en['promoter_type']);
		}
		
		if(!empty($promoter_en['promoter_id'])){
			$condition_array['promoter_id'] = array('like', "%".$promoter_en['promoter_id']."%");
		}
		
		if(!empty($promoter_en['promoter_name'])){
			$condition_array['promoter_name'] = array('like', "%".$promoter_en['promoter_name']."%");
		}
		// 搜索条件end
	
		
		$return_en = array();
		
		$total_count = M('promoterInfo')
		->where($condition_array)
		->order('create_date DESC')
		->count();
		$return_en['total_count'] = $total_count;
		
		
		$lst_promoter_en = M('promoterInfo')
		->page($promoter_en['page_index'], $promoter_en['page_size'])
		->order('create_date DESC')
		->where($condition_array)
		->select();
		
		foreach($lst_promoter_en as $key => $en){
			$lst_promoter_en[$key]['promoter_type_text'] = StringBiz::getPromoterTypeText($en['promoter_type']);
			$lst_promoter_en[$key]['status_text'] = StringBiz::getPromoterInfoStatusText($en['status']);
		}
		$return_en['lst_promoter_en'] = $lst_promoter_en;
	
		$total_page = 0;
		$rem = $total_count % $promoter_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $promoter_en['page_size'];
		}
		else{
			$total_page = floor($total_count / $promoter_en['page_size']) + 1;
		}
		$return_en['total_page'] = $total_page;
		
		return $return_en;
	}
	
	
	public static function updateAmount($param_en, $user=''){
		// 参数检查start
		if(!is_numeric($param_en['amount'])){
			return '请输入正确的金额';
		}
		
		
		if(empty($param_en['promoter_id'])){
			return '请输入推广人编号';
		}
		
		if($param_en['amount_type'] == 11){
			$param_en['amount_kind'] = 1;
		}
		
		if($param_en['amount_type'] == 21){
			$param_en['amount_kind'] = 2;
		}
		
		if(empty($param_en['amount_kind'])){
			return '选择了错误的变更类型';
		}
		
		
		$promoter_en = M('promoterInfo')->where(array('promoter_id'=>$param_en['promoter_id']))->find();
		
		if(empty($promoter_en)){
			return '不存在的推广人';
		}
		// 参数检查end
		
		$amount		  = floatval($param_en['amount']);
		$begin_amount = $promoter_en['amount'];
		
		$remark = '';
		$withdraw_flag = 0;
		if($param_en['amount_kind'] == 1){
			$end_amount	= floatval($promoter_en['amount']) + floatval($param_en['amount']);
			$remark		= empty($param_en['remark']) ? '客服增加'.$amount.'元' : $remark;
			$withdraw_flag = 0;
		}
		else{
			$end_amount	= floatval($promoter_en['amount']) - floatval($param_en['amount']);
			$remark		= empty($param_en['remark']) ? '客服减少'.$amount.'元' : $remark;
			$withdraw_flag = 1;
		}
		
		
		M('promoterInfo')->where(array('promoter_id'=>$param_en['promoter_id']))->setField('amount', $end_amount);
		
		
		$add_amount_en = array(
				'promoter_id'		=> $promoter_en['promoter_id'],
				'promoter_name'		=> $promoter_en['promoter_name'],
				'amount_kind'		=> $param_en['amount_kind'],
				'amount_type'		=> $param_en['amount_type'],
				'amount'			=> $amount,
				'begin_amount'		=> $begin_amount,
				'end_amount'		=> $end_amount,
				'remark'			=> $remark,
				'withdraw_flag'		=> $withdraw_flag,
				'status'			=> 1,
				'create_user'		=> $user,
    			'create_date'		=> date("y-m-d H:i:s", time()),
		);
		M('promoterAmount')->add($add_amount_en);
		
		
		if($withdraw_flag == 1){
			//如果是扣减金额，需要同时扣减推广人的可提现金额
			$begin_withdraw_amount 	= $promoter_en['withdraw_amount'];
			$end_withdraw_amount	= $promoter_en['withdraw_amount'] - $amount;
			
			//2.将数据写入promoter_withdraw_amount
			$add_promoter_withdraw_amount_en = array(
					'promoter_id'	=> $promoter_en['promoter_id'],
					'promoter_name'	=> $promoter_en['promoter_name'],
					'amount_kind'	=> $param_en['amount_kind'],
					'amount_type'	=> $param_en['amount_type'],
					'withdraw_amount'		=> $amount,
					'begin_withdraw_amount'	=> $begin_withdraw_amount,
					'end_withdraw_amount'	=> $end_withdraw_amount,
					'remark'		=> $remark,
					'status'		=> 1,
					'create_user'	=> $user,
					'create_date'	=> date("y-m-d H:i:s", time()),
			);
			M('promoterWithdrawAmount')->add($add_promoter_withdraw_amount_en);
			
			M('promoterInfo')->where(array('promoter_id'=>$param_en['promoter_id']))->setField('withdraw_amount', $end_withdraw_amount);
		}
		
	}
	
	
	public static function updatePromoterStatus($promoter_id,$reject_reason, $status){
		// 参数检查start
		
		$promoter_en = M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->find();
		
		if(empty($promoter_en)){
			return '不存在的推广人';
		}
		// 参数检查end
		$add_promoter_status_change_log = array(
				'promoter_id'	=>$promoter_id,
				'status'	=> $status,
				'status_text'	=> StringBiz::getPromoterInfoStatusText($status),
				'remark'	=> $reject_reason,
				'create_user'	=> "whaleyFz.admin PromoterBiz.updatePromoterStatus",
				'create_date'	=> date("y-m-d H:i:s", time())
		);
		M('promoter_status_change_log')->add($add_promoter_status_change_log);
		
		
		M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->setField('status', $status);
		
	}
	
	
	/**
	 * 提现
	 */
	public static function withdraw($promoter_id, $withdraw_amount, $user){
		// 数据校验start
		if(!is_numeric($withdraw_amount)){
			return '输入了错误的金额格式';
		}
		
		if($withdraw_amount == 0){
			return '提现金额需大于0';
		}
		
		$promoter_en = M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->find();
		if(empty($promoter_en)){
			return '用户不存在';
		}
		else{
			if($promoter_en['status'] != 1){
				return '用户状态无效，不允许提现';
			}
		}
		
		$curr_amount 			= $promoter_en['amount'];
		$curr_withdraw_amount 	= $promoter_en['withdraw_amount'];
		if($curr_amount < $withdraw_amount){
			return '提现金额大于当前用户余额';
		}
		
		if($curr_amount < $withdraw_amount){
			return '提现金额大于当前用户可提现余额';
		}
		// 数据校验end
		
		//新增提现申请数据
		$add_promoter_withdraw_en = array(
				'promoter_id'		=> $promoter_en['promoter_id'],
				'promoter_name' 	=> $promoter_en['promoter_name'],
				'withdraw_amount'	=> $withdraw_amount,
				'submit_date'		=> date("y-m-d H:i:s", time()),
				'status'			=> 1,
				'create_user'		=> 	$user,
    			'create_date'		=>	date("y-m-d H:i:s", time()),
		);
		M('promoterWithdraw')->add($add_promoter_withdraw_en);
		
		
		//更改用户账户余额和可提现余额，并写入明细start
		$update_promoterInfo_en = array(
				'amount'			=> $curr_amount - $withdraw_amount,
				'withdraw_amount'	=> $curr_withdraw_amount - $withdraw_amount,
		);
		
		M('promoterInfo')->where(array('promoter_id'=>$promoter_id))->save($update_promoterInfo_en);
		
		
		$add_promoter_amount_en = array(
				'promoter_id'	=> $promoter_en['promoter_id'],
				'promoter_name' => $promoter_en['promoter_name'],
				'amount_kind'	=> 2,
				'amount_type'	=> 22,
				'amount'		=> $withdraw_amount,
				'begin_amount'	=> $curr_amount,
				'end_amount'	=> $curr_amount - $withdraw_amount,
				'status'		=> 1,
				'create_user'		=> 	$user,
				'create_date'		=>	date("y-m-d H:i:s", time()),
		);
		M('promoterAmount')->add($add_promoter_amount_en);
		
		$add_promoter_withdraw_amount_en = array(
				'promoter_id'			=> $promoter_en['promoter_id'],
				'promoter_name' 		=> $promoter_en['promoter_name'],
				'amount_kind'			=> 2,
				'amount_type'			=> 22,
				'withdraw_amount'		=> $withdraw_amount,
				'begin_withdraw_amount'	=> $curr_amount,
				'end_withdraw_amount'	=> $curr_withdraw_amount - $withdraw_amount,
				'status'				=> 1,
				'create_user'		=> 	$user,
				'create_date'		=>	date("y-m-d H:i:s", time()),
		);
		M('promoterWithdrawAmount')->add($add_promoter_withdraw_amount_en);
		//更改用户账户余额和可提现余额，并写入明细end
	}
	
	
	public static function generateWithdrawDetailText($lst_promoter_withdraw_en){
		
		
		$all_detail_array = array();
		foreach($lst_promoter_withdraw_en as $promoter_withdraw_en){
			$detail_array = array();
			array_push($detail_array, $promoter_withdraw_en['id']);
			array_push($detail_array, $promoter_withdraw_en['alipay_id']);
			array_push($detail_array, $promoter_withdraw_en['alipay_name']);
			array_push($detail_array, $promoter_withdraw_en['withdraw_amount']);
			array_push($detail_array, '序号：'.$promoter_withdraw_en['id'].'。英雄会提现'.$promoter_withdraw_en['withdraw_amount'].'元');
			$detail_array_str = implode('^', $detail_array);
			
			array_push($all_detail_array, $detail_array_str);
		}
		
		$detail_data	= implode('|', $all_detail_array);
		return $detail_data;
	}




    #region ========= 分成与返现逻辑 =========
    /**
     * 返现到所有用户账户
     */
    public static function revenueAll(){
        header("Content-Type: text/html; charset=UTF-8");
        $order_id = I('order_id');

        if(empty($order_id)){
            $lst_revenue_promoter_order = M('promoterOrder')
                ->where("order_revenue_status = 0 AND order_status = 35 AND payment_status = 1")
                ->select();
        }
        else{
            $lst_revenue_promoter_order = M('promoterOrder')
                ->where("order_id='$order_id' AND order_revenue_status = 0 AND order_status = 35 AND payment_status = 1")
                ->select();
        }



        $lst_sku_en = M('sku', '', 'DB_trade')->where("is_buy_fz=1")->select();
        foreach($lst_revenue_promoter_order as $promoter_order_en){


            $promoter_id 	 	= $promoter_order_en['promoter_id'];
            $lst_promoter_order_detail	= M('promoter_order_detail')->where("order_id='%s'", array($promoter_order_en['order_id']))->select();

            $order_revenue_amount = 0;
            foreach($lst_promoter_order_detail as $promoter_order_detail_en){
                $qty			            = $promoter_order_detail_en['qty'];


                $revenue_amount = PromoterBiz::revenueToPseron($promoter_id,
                    $promoter_order_en,
                    $promoter_order_detail_en,
                    $qty);

                $order_revenue_amount = $order_revenue_amount + $revenue_amount;
            }


            //更新用户promoterInfo的余额
            $promoterInfo_en 		= M('promoterInfo')->where("promoter_id='%s'", array($promoter_id))->find();
            $promoterInfo_amount	= $promoterInfo_en['amount'];
            $new_promoterInfo_amount= $promoterInfo_amount + $order_revenue_amount;
            M('promoterInfo')->where("promoter_id='%s'", array($promoter_id))->setField('amount', $new_promoterInfo_amount);


            //写入用户余额变更
            $add_promoter_amount_en = array(
                'promoter_id'		=>	$promoter_order_en['promoter_id'],
                'promoter_name'		=>	$promoter_order_en['promoter_name'],
                'amount_kind'		=>	1,
                'amount_type'		=>	12,
                'amount'			=>	$order_revenue_amount,
                'begin_amount'		=>	$promoterInfo_amount,
                'end_amount'		=>	$new_promoterInfo_amount,
                'order_id'			=>	$promoter_order_en['order_id'],
                'remark'			=>	'订单'.$promoter_order_en['order_id'].'分成'.strval($order_revenue_amount).'元',
                'withdraw_flag'		=>	0,
                'status'			=>	1,
                'create_user'			=> 'whaleyFZ.admin revenueAll',
                'create_date'			=> date("y-m-d H:i:s", time()),
            );
            M('promoterAmount')->add($add_promoter_amount_en);


            //更新promoterOrder相应状态
            $update_promoterOrder_en = array(
                'order_revenue_status'	=> 3,
                'revenue_amount'		=> $order_revenue_amount
            );

            M('promoterOrder')->where("order_id='%s'", array($promoter_order_en['order_id']))->save($update_promoterOrder_en);
        }

        return count($lst_revenue_promoter_order);
    }


    /**
     * 返现到个人账户
     * @param $promoter_id
     * @param $promoter_order_en
     * @param $promoter_order_detail_en
     * @param $qty
     * @return int
     */
    public static function revenueToPseron($promoter_id, $promoter_order_en, $promoter_order_detail_en, $qty){

        //此订单中商品需分成的金额
        $revenue_amount = 0;
        $revenue_amount = $promoter_order_detail_en['person_commission_amount'] * $qty;

        //写入分成明细
        $add_promoter_order_revenue_detailEn = array(
            'order_id'			=>	$promoter_order_en['order_id'],
            'third_order_id'	=>	$promoter_order_en['third_order_id'],
            'promoter_id'		=>	$promoter_order_en['promoter_id'],
            'promoter_name'		=>	$promoter_order_en['promoter_name'],
            'promoter_type'		=>	$promoter_order_en['promoter_type'],
            'qty'				=>	$qty,
            'revenue_type'		=>	1,
            'revenue_amount'	=>  $revenue_amount,
            'sku_sn'			=>	$promoter_order_detail_en['sku_id'],
            'sku_name'			=>	$promoter_order_detail_en['sku_name'],
            'style_sn'			=>	$promoter_order_detail_en['style_id'],
            'style_name'		=>	$promoter_order_detail_en['style_name'],
            'person_commission_amount'	=>	$promoter_order_detail_en['person_commission_amount'],
            'dealer_commission_amount'	=>	$promoter_order_detail_en['dealer_commission_amount'],
            'status'			=>	1,
            'create_user'			=> 'whaleyFZ.admin revenueAll',
            'create_date'			=> date("y-m-d H:i:s", time()),
        );
        M('promoterOrderRevenueDetail')->add($add_promoter_order_revenue_detailEn);

        return $revenue_amount;
    }



    /**
     * 转为可提现 Start
     */
    public static function ConvertWithdrawAll(){
        header("Content-Type: text/html; charset=UTF-8");

        //1.获取所有需转入的推广人余额
        $lst_promoterAmount_en = M('promoterAmount')->where(array('withdraw_flag'=>0))->select();


        //2.将推广人余额写入可提现余额
        foreach($lst_promoterAmount_en as $promoterAmount_en){
            switch($promoterAmount_en['amount_type']){
                case 12:
                    //订单提成，需7日后进入可提现余额
                    $convert_flag   = false;
                    if(empty($promoterAmount_en['order_id'])){
                        $date1 = date_create($promoterAmount_en['create_date']);
                        $date2 = date_create();
                        $diff  = date_diff($date1,$date2);
                        $interval_hours = $diff->days * 24 + $diff->h;
                        if($interval_hours > 7*24){
                            $convert_flag   = true;
                        }
                    }
                    else{
                        $order_id   = $promoterAmount_en['order_id'];
                        $order_date = M('orderInfo', '', 'DB_trade')->where("order_id='%s'", $order_id)->getField('order_date');

                        $date1 = date_create(date("y-m-d H:i:s", $order_date));
                        $date2 = date_create();
                        $diff  = date_diff($date1,$date2);
                        $interval_hours = $diff->days * 24 + $diff->h;
                        if($interval_hours > 12*24){
                            $convert_flag   = true;
                        }
                    }


                    if($convert_flag == true){
                        PromoterBiz::ConvertWithdrawAmount($promoterAmount_en);
                    }
                    break;
                default:
                    PromoterBiz::ConvertWithdrawAmount($promoterAmount_en);
                    break;
            }

        }

        return count($lst_promoterAmount_en);
    }


    public static function ConvertWithdrawAmount($promoterAmount_en){
        //1.更改用户可提现余额
        $promoterInfo_en = M('promoterInfo')->where(array('promoter_id'=>$promoterAmount_en['promoter_id']))->find();
        if(empty($promoterInfo_en)){
            return;
        }

        $withdraw_amount 		= $promoterAmount_en['amount'];
        $begin_withdraw_amount 	= $promoterInfo_en['withdraw_amount'];
        $end_withdraw_amount	= 0;
        if($promoterAmount_en['amount_kind'] == 1){
            $end_withdraw_amount	= $begin_withdraw_amount + $withdraw_amount;
        }
        else{
            $end_withdraw_amount	= $begin_withdraw_amount - $withdraw_amount;
        }


        M('promoterInfo')->where(array('promoter_id'=>$promoterAmount_en['promoter_id']))->setField('withdraw_amount', $end_withdraw_amount);

        //2.将数据写入promoter_withdraw_amount
        $add_promoter_withdraw_amount_en = array(
            'promoter_id'	=> $promoterAmount_en['promoter_id'],
            'promoter_name'	=> $promoterAmount_en['promoter_name'],
            'amount_kind'	=> $promoterAmount_en['amount_kind'],
            'amount_type'	=> $promoterAmount_en['amount_type'],
            'withdraw_amount'		=> $withdraw_amount,
            'begin_withdraw_amount'	=> $begin_withdraw_amount,
            'end_withdraw_amount'	=> $end_withdraw_amount,
            'order_id'		=> $promoterAmount_en['order_id'],
            'remark'		=> $promoterAmount_en['remark'],
            'status'		=> 1,
            'create_user'	=> 'whaleyFZ.admin ConvertWithdrawAll',
            'create_date'	=> date("y-m-d H:i:s", time()),
        );
        M('promoterWithdrawAmount')->add($add_promoter_withdraw_amount_en);


        //3.更改当前数据的状态
        M('promoterAmount')->where(array('id'=>$promoterAmount_en['id']))->setField('withdraw_flag', 1);
    }
    #endregion ========= 分成与返现逻辑 =========





}